//
//  GroupListCell.swift
//  eemall
//
//  Created by zhuxietong on 2018/12/24.
//  Copyright © 2018 tianrun. All rights reserved.
//

import UIKit
import AsyncDisplayKit
import soapp


extension Goods
{
    
    public enum GroupPurchaseProcess{
        case prepare(tag:String)
        case willStart(tag:String)//剩余时间
        case begined(tag:String)
        case finished(tag:String)
        case invalid
        
        public static func create(begin:Date,end:Date)->GroupPurchaseProcess{
            
            var process:GroupPurchaseProcess = .invalid
            let beginInfo = begin.timeSince()
            if beginInfo.0 == .later
            {
                if begin["YYYYMMdd"] == Date()["YYYYMMdd"]
                {
                    process = GroupPurchaseProcess.willStart(tag: beginInfo.1)
                }
                else{
                    process = GroupPurchaseProcess.prepare(tag: begin["yyyy年MM月dd日 MM:hh"])
                }
            }
            else
            {
                let endInfo = end.timeSince()
                if endInfo.0 == .later
                {
                    process = GroupPurchaseProcess.begined(tag: endInfo.1)
                }
                else
                {
                    process = GroupPurchaseProcess.finished(tag: end["yyyy年MM月dd日 MM:hh"])
                }
            }
            return process
            
        }
        
        
        public var name:(name:String,color:UIColor)
        {
            get{
                switch self {
                case .prepare(_):
                    return ("拍卖".loc,___shop.tint)
                case .willStart(_):
                    return ("即将开拍".loc,___shop.tint)
                case .begined(_):
                    return ("拍卖中".loc,___shop.tint)
                case .finished(_):
                    return ("拍卖结束".loc,._disable)
                default:
                    return ("已失效".loc,._disable)
                }
            }
        }
    }

    open class GroupListCell: BaseCell {
        
        public let head1 = JoImageNode()
        public let head2 = JoImageNode()
        
        public let group_priceL = ASTextNode2()
        public let group_numL = ASTextNode2()

        
        open override func addLayoutRules() {
            head1.ui.holder(type: .head)
            head2.ui.holder(type: .head)
            head1.bsui.background(color: UIColor.table_bg_color)
            head2.bsui.background(color: UIColor.table_bg_color)
            

        }
        
        
      
        
        open override func didLayoutRules() {
            observerControllerAction()
        }
        
        override open func mo_model_reload() {
            super.mo_model_reload()
            
            let singleprice = mo.model[float:"singleprice",0]
            let groupsprice = mo.model[float:"groupsprice",0]
            
            
            group_priceL.attributedText = "¥ ".ui.font12.color(color: ._foreground).astring + "\(groupsprice.price_display)".ui.bfont15.color(color:___shop.tint).astring
            
        
            priceL.attributedText = "单买价格  ".ui.font10.color(color: ._foreground).astring + "¥ ".ui.font10.color(color: ._foregroundWeak).astring + "\(singleprice.price_display)".ui.font13.color(color: ._foreground).astring
            
//            let groupnum = mo.model[int:"groupnum",0]
            
//            print("====DD",mo.model)
            let groupName = "[XX] 人团购".replaceVars(values: mo.model["groupnum","0"])
            let name = mo.model["is_ladder",false.tag] == true.tag ? "阶梯团".loc : groupName
            
            activeBt.setAttributedTitle(name.ui.font11.cl_fff.astring, for: .normal)
            activeBt.contentEdgeInsets = [3,12]
            activeBt.backgroundColor = UIColor(shex: AppTheme.main_hex)            
            
            let fightnum = mo.model[int:"fightnum",0]
            if fightnum > 0{
                group_numL.attributedText = "[XX]人正在拼团".replaceVars(values: "\(fightnum)").ui.font12.color(color: ._foregroundWeak).astring
            }
            else{
                group_numL.attributedText = "火热拼团中".loc.ui.font12.color(color: ._foregroundWeak).astring
            }


            head1.img_url = mo.model["teams.0.avatar",""]
            head2.img_url = mo.model["teams.1.avatar",""]

            
            head1.view.bsui.set(radius: 10, width: 1, color: UIColor(shex: "#eeec"))
            head2.view.bsui.set(radius: 10, width: 1, color: UIColor(shex: "#eeec"))

//            mo.model["teams"] = [
//                ["img":__heads.one_str],
//                ["img":__heads.one_str],
//            ].mutable_array
            
            
            
        }

 
     
        
        open override var rightSpec:_Spec{
            get{
                var group = [_Spec]()
                let teams = DataNode.path(node: "teams", mo.model, value: NSMutableArray())

                teams.list { (one:NSMutableDictionary, i) in
                    if i == 0
                    {
                        group.append(head1["20",20])
                    }
                    if i == 1
                    {
                        group.append(head2["20",20,[0,-10,0,0]])
                    }
                }
                group.append(group_numL[_grow(1),_shrink(1)])
                
                let bar:_Spec = .stack(["v|8",_grow(1),_shrink(1),&-.stretch,_justfy(.space_between)], childs: [
                    nameL.Spec,
                    .stack(["h|5",&-.center], childs: group),
                    group_priceL.Spec,
                    .stack(["h",&-.center,_justfy(.space_between)], childs: [
                        priceL.Spec,
//                        buyNumL.Spec,
                        ]),
//                    .stack(["h|10",&-.center], childs: [processNameL["20",[4,0,0,0]],processTimeL.Spec])
                    ])
                return bar
            }
        }
    }

}
